package cn.rui.service.impl;

import cn.rui.common.Result;
import cn.rui.mapper.OperationLogMapper;
import cn.rui.pojo.OperationLog;
import cn.rui.pojo.vo.PageResult;
import cn.rui.service.OperationLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2021/05/17
 */
@Service
@Transactional
@EnableAsync
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    @Async
    public void save(OperationLog operationLog) {
        operationLogMapper.insert (operationLog);
    }

    @Override
    public Map findPage(PageResult pageResult) {

        QueryWrapper<OperationLog> wrapper = new QueryWrapper<> ();
        wrapper.orderByDesc ("create_time");
        Page<OperationLog> page = new Page<> (pageResult.getCurrentPage (), pageResult.getPageSize ());
        IPage<OperationLog> operationLogIPage = operationLogMapper.selectPage (page, wrapper);
        Map map = new HashMap ();
        map.put ("data", operationLogIPage.getRecords ());
        map.put ("total", operationLogIPage.getTotal ());
        map.put ("flag", true);
        return map;
    }

    @Override
    public Result deleteById(Integer id) {
        operationLogMapper.deleteById (id);
        return Result.getSuccess ("删除成功!");
    }
}
